//用户数据

var redis=require('./Redis');
var db=require('./db');

function User(name, mail,isAdmin) {
    this.name = name;
    this.mail = mail;
    this.isAdmin=isAdmin;
};


//插入数据
User.prototype.insert=function(callback){
    var obj={};
    obj["name"]=this.name;
    obj["mail"]=this.mail;
    obj["isAdmin"]=this.isAdmin;
    redis.get("user.max_id",function(err,reply){
        if(err) return callback(err);
        var id=parseInt(reply)+1;
        var key="user:"+id;
        obj["id"]=id;
        console.log("插入的数据:"+JSON.stringify(obj));
        redis.set(key,JSON.stringify(obj));
        redis.incr("user.max_id");
        callback(null);
    })
}

//更新数据
User.prototype.update=function(userId,callback){
    var obj={};
    obj["id"]=userId;
    obj["name"]=this.name;
    obj["mail"]=this.mail;
    obj["isAdmin"]=this.isAdmin;

    var key="user:"+userId;
    redis.set(key,JSON.stringify(obj),function(err,reply){
        console.log("reply:"+reply);
        callback(err);
    });
}



User.all = function(callback) {
    redis.keys("user:*",function(err,keys){
        //没有key值也会返回err
        if(err) return callback(err);
        redis.mget(keys,function(error,data){
            return callback(error,data);
        })
    })
}

User.getUserInfoByMail=function(mail,callback){
    User.all(function(err,users){
        var result=null,users=users||[];
        for(var i=0;i<users.length;i++){
            var user=JSON.parse(users[i]);
            if(user.mail==mail){
                result=user;
                break;
            }
        }
        return callback(err,result);
    });
}


//根据id查找用户数据
User.findById=function(id,callback){
    var key="user:"+id;
    redis.get(key,function(err,user){
        callback(err,user);
    })
}

//根据id删除数据
User.deleteById=function(id,callback){
    var key="user:"+id;
    redis.del(key,function(err,reply){
        console.log("reply:"+reply);
        callback(err);
    })
}

//同步数据
User.syncData=function(){
    db.query("select * from `user` order by `id` desc",function(err,rows){
        if(err){
            console.error("执行%s语句数据错误,错误详情:"+err,sql);
        }
        else{
            redis.keys("user:*",function(err,keys){
                if(keys){
                    redis.del(keys,function(err,reply){
                        for(var i=0;i<rows.length;i++){
                            var data=rows[i];
                            var key="user:"+data["id"];
                            redis.set(key,JSON.stringify(data));
                        }
                    })
                }
                else{
                    for(var i=0;i<rows.length;i++){
                        var data=rows[i];
                        var key="user:"+data["id"];
                        redis.set(key,JSON.stringify(data));
                    }
                }
            })
        }
        redis.set("user.max_id",(rows&&rows[0])?rows[0].id:0);
    });
}

//同步Redis数据到数据库
User.syncDataToSql=function(){
    User.all(function(err,users){
        if(err) return console.log("从Redis获取用户数据失败");
        db.query('delete from `user` where 1=1',function(e,reply){
           if(err) return console.log("清空user表出错");
           for(var i=0;i<users.length;i++){
                var u=JSON.parse(users[i]);
                console.log(users[i]);
                //插入数据库
                db.query('insert into `user` (`id`,`name`,`mail`,`isAdmin`) values (?,?,?,?)',[u.id,u.name,u.mail,u.isAdmin]);
            }
        });
        
    })
}

module.exports = User;
